package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import redis.clients.jedis.JedisPool;

import java.util.Arrays;
import java.util.List;

/**
 * @author Autism
 * @version V1.0
 * @Package com.itheima.service
 * @date 2019/7/29 15:53
 * @Description
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealDao setmealDao;

    @Autowired
    JedisPool jedisPool;

    /**
     * 新增套餐
     *
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        //新增基本信息
        setmealDao.add(setmeal);
        Integer id = setmeal.getId();
        //关联套餐和检查组的信息
        List<Integer> checkGroupIdList = Arrays.asList(checkgroupIds);
        if (!CollectionUtils.isEmpty(checkGroupIdList)) {
            checkGroupIdList.forEach(checkGroupId ->{
                setmealDao.addRelations(id, checkGroupId);
            });
        }
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
    }

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Setmeal> pages = setmealDao.findPage(queryString);
        return new PageResult(pages.getTotal(), pages.getResult());
    }
}
